package com.fescoservice.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Link;

/**
 * A data access object (DAO) providing persistence and search support for Link
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fescoservice.model.Link
 * @author MyEclipse Persistence Tools
 */

public class LinkDAO extends HibernateDaoSupport {
	private static final Logger log = LogManager.getLogger(LinkDAO.class);
	// property constants
	public static final String LINK_TITLE = "linkTitle";
	public static final String LINK_URL = "linkUrl";
	public static final String MEMO = "memo";
	public static final String LINK_TYPE = "linkType";
	public static final String LINK_DEPT = "linkDept";
	public static final String OTHER_FIELD1 = "otherField1";
	public static final String OTHER_FIELD2 = "otherField2";
	public static final String OTHER_FIELD3 = "otherField3";

	protected void initDao() {
		// do nothing
	}

	public void save(Link transientInstance) {
		log.debug("saving Link instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Link persistentInstance) {
		log.debug("deleting Link instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Link findById(java.lang.Integer id) {
		log.debug("getting Link instance with id: " + id);
		try {
			Link instance = (Link) getHibernateTemplate().get(
					"com.fescoservice.model.Link", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
	public Integer countLinks(final String sql,final String language,final String dept) throws FSException{
		log.debug("finding Category instance by example");
		try {
			Integer count = (Integer) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {						   
						   SQLQuery q = session.createSQLQuery(sql);
						   q.setString("dept",dept);
						   if(null!=language){
							   q.setString("language",language);
						   }
						  return q.addScalar( "cnt" , Hibernate.INTEGER).uniqueResult();
					   }
					 } );
			return count;
		} catch (Exception re) {
			log.error("count links failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}
	
	public List listLinks(final String sql,final String language,final String dept,final int first) throws FSException{
		log.debug("finding Category instance by example");
		try {
			List result = (List) getHibernateTemplate().execute(
					   new HibernateCallback() {
					   public Object doInHibernate(Session session)
					   throws HibernateException, SQLException {						   
						   Query q = session.createQuery(sql);
						   q.setString("dept",dept);
						   if(null!=language){
							   q.setString("language",language);
						   }
						   q.setFirstResult(first);
						   q.setMaxResults(PropertiesConfig.getInstance().getInt("ListSize.Link"));
						  return q.list();
					   }
					 } );
			return result;
		} catch (Exception re) {
			log.error("list links failed", re);
			throw new FSException("FS_ERROR_COUNTCATGORY",re.getMessage());
		}
	}

	public List findByExample(Link instance) {
		log.debug("finding Link instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Link instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Link as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByLinkTitle(Object linkTitle) {
		return findByProperty(LINK_TITLE, linkTitle);
	}

	public List findByLinkUrl(Object linkUrl) {
		return findByProperty(LINK_URL, linkUrl);
	}

	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	public List findByLinkType(Object linkType) {
		return findByProperty(LINK_TYPE, linkType);
	}

	public List findByLinkDept(Object linkDept) {
		return findByProperty(LINK_DEPT, linkDept);
	}

	public List findByOtherField1(Object otherField1) {
		return findByProperty(OTHER_FIELD1, otherField1);
	}

	public List findByOtherField2(Object otherField2) {
		return findByProperty(OTHER_FIELD2, otherField2);
	}

	public List findByOtherField3(Object otherField3) {
		return findByProperty(OTHER_FIELD3, otherField3);
	}

	public List findAll() {
		log.debug("finding all Link instances");
		try {
			String queryString = "from Link";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Link merge(Link detachedInstance) {
		log.debug("merging Link instance");
		try {
			Link result = (Link) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Link instance) {
		log.debug("attaching dirty Link instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Link instance) {
		log.debug("attaching clean Link instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static LinkDAO getFromApplicationContext(ApplicationContext ctx) {
		return (LinkDAO) ctx.getBean("LinkDAO");
	}
}